﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
///DbClass 的摘要说明
/// </summary>
public class DbClass
{
	static public SqlConnection GetSqlConnection()
	{
		string connectionString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
							
		try
		{
			SqlConnection connection = new SqlConnection(connectionString);
			connection.Open();

			return connection;
		}
		catch (Exception exception)
		{
			
			throw new Exception(exception.Message);
		}
		
	}

	static public void ExecuteNonQuery(SqlConnection connection, string commmandText)
	{
		try
		{
			SqlCommand command = new SqlCommand(commmandText, connection);
			command.ExecuteNonQuery();
		}
		catch (Exception exception)
		{

			throw new Exception(exception.Message);
		}
	}

	static public object ExecuteScalar(SqlConnection connection, string commmandText)
	{
		try
		{
			SqlCommand command = new SqlCommand(commmandText, connection);
			
			return command.ExecuteScalar();
		}
		catch (Exception exception)
		{

			throw new Exception(exception.Message);
		}
	}

	static public SqlDataReader GetSqlDataReader(SqlConnection connection, string commmandText)
	{
		try
		{
			SqlCommand command = new SqlCommand(commmandText, connection);
			SqlDataReader dataReader = command.ExecuteReader();

			return dataReader;
		}
		catch (Exception exception)
		{

			throw new Exception(exception.Message);
		}
	}


	static public DataTable GetDataTable(SqlConnection connection, string commmandText)
	{
		try
		{
			SqlDataAdapter da = new SqlDataAdapter(commmandText, connection);

			DataSet ds = new DataSet();
			da.Fill(ds);
			return ds.Tables[0];
		}
		catch (Exception exception)
		{

			throw new Exception(exception.Message);
		}
	}

	
}
